package xcmg.device.web.basic;


import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.basic.ExcipientBindingInfoService;
import xcmg.device.service.vo.basic.ExcipientBindingAddVO;
import xcmg.device.service.vo.basic.ExcipientBindingInfoVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;
import yb.ecp.fast.infra.infra.log.LogHelper;

/**
 * @author wangyong
 */
@RestController
@RequestMapping("/excipient")
public class ExcipientBindingInfoController extends BasicController {

  @Autowired
  private ExcipientBindingInfoService excipientBindingInfoService;


  @RequestMapping(value = "/listByMaterialsNo", method = RequestMethod.GET)
  @FastMappingInfo(needLogin = true)
  public ActionResult listByMaterialsNo(String materialsNo) {
    ActionResult result = null;
    try {
      result = actionResult(ErrorCode.Success,
          excipientBindingInfoService.listByMaterialsNo(materialsNo, getOrgId()));
    } catch (Exception e) {
      LogHelper.fatal("ExcipientBindingInfoController#listByMaterialsNo is erroe.", e);
      return actionResult(ErrorCode.Failure, e.getMessage());

    }
    return result;
  }

  @RequestMapping(value = "/insert", method = RequestMethod.POST)
  @ApiOperation(value = "添加绑定关系")
  @FastMappingInfo(needLogin = true)
  public ActionResult insert(@RequestBody ExcipientBindingAddVO excipientBindingAddVO) {

    ActionResult result = null;
    try {

      result = excipientBindingInfoService.insert(excipientBindingAddVO, getUserId(), getOrgId());
    } catch (Exception e) {
      LogHelper.fatal("ExcipientBindingInfoController#insert is error.", e);
      return actionResult(ErrorCode.Failure, e.getMessage());
    }
    return result;
  }

  @RequestMapping(value = "/list", method = RequestMethod.POST)
  @ApiOperation(value = "辅料绑定关系列表")
  @FastMappingInfo(needLogin = true)
  public ActionResult list(@RequestBody SearchCommonVO<ExcipientBindingInfoVO> condition)
      throws Exception {

    ExcipientBindingInfoVO excipientBindingInfoVO = condition.getFilters();
    if (excipientBindingInfoVO == null) {
      excipientBindingInfoVO = new ExcipientBindingInfoVO();
    }
    excipientBindingInfoVO.setOrgId(getOrgId());
    condition.setFilters(excipientBindingInfoVO);
    PageCommonVO pageCommonVO = excipientBindingInfoService.list(condition);
    return actionValueResult(pageCommonVO.getPageInfo());
  }

  @RequestMapping(value = "/delete", method = RequestMethod.POST)
  @ApiOperation(value = "删除辅料绑定关系")
  @FastMappingInfo(needLogin = true, code = AuthCode.ThreeGuarantees.DEL)
  public ActionResult remove(@RequestBody String[] ids) throws Exception {
    if (ids == null || ids.length == 0) {
      return actionResult(ErrorCode.IllegalArument);
    }
    ActionResult result = null;
    try {
      result = excipientBindingInfoService.remove(Arrays.asList(ids));
    } catch (Exception e) {
      actionResult(ErrorCode.Failure, e.getMessage());
      LogHelper.fatal("ExcipientBindingInfoController#remove is error.", e);
    }
    return result;
  }


  @RequestMapping(value = "/update", method = RequestMethod.POST)
  @ApiOperation(value = "修改辅料绑定关系")
  @FastMappingInfo(needLogin = true)
  public ActionResult update(@RequestBody ExcipientBindingAddVO excipientBindingAddVO)
      throws Exception {

    ActionResult result = null;
    try {
      result = excipientBindingInfoService.update(excipientBindingAddVO, getUserId(), getOrgId());
    } catch (Exception e) {
      LogHelper.fatal("ExcipientBindingInfoController#update is error.", e);
      return actionResult(ErrorCode.Failure, e.getMessage());
    }
    return result;
  }

  @RequestMapping(value = "/listExcipientsData", method = RequestMethod.POST)
  @ApiOperation(value = "获取报表数据")
  @FastMappingInfo(needLogin = true)
  public ActionResult listExcipientsData(
      @RequestBody SearchCommonVO<ExcipientBindingInfoVO> condition) throws Exception {
    ExcipientBindingInfoVO excipientBindingInfoVO = condition.getFilters();
    if (excipientBindingInfoVO == null) {
      excipientBindingInfoVO = new ExcipientBindingInfoVO();
    }
    excipientBindingInfoVO.setOrgId(getOrgId());
    condition.setFilters(excipientBindingInfoVO);
    PageCommonVO pageCommonVO = excipientBindingInfoService.listReportData(condition);
    return actionValueResult(pageCommonVO.getPageInfo());
  }


  @RequestMapping(value = "/exportAllConfig",method = RequestMethod.POST)
  @FastMappingInfo(needLogin = true,code = AuthCode.SaleIntention.EXPORT)
  public void exportAllConfig(ExcipientBindingInfoVO condition)throws Exception {

    condition.setOrgId(getOrgId());
    excipientBindingInfoService.exportAllConfig(condition);
  }

  @RequestMapping(value = "/exportAllReport",method = RequestMethod.POST)
  @FastMappingInfo(needLogin = true,code = AuthCode.SaleIntention.EXPORT)
  public void exportAllReport(ExcipientBindingInfoVO condition)throws Exception {
    condition.setOrgId(getOrgId());
    excipientBindingInfoService.exportAllReport(condition);
  }

}
